/**
 * 
 */
package com.googlecode.lohr.parser.combinators.matcher;


import java.util.Arrays;

import com.googlecode.lohr.parser.combinators.MatchResults;
import com.googlecode.lohr.parser.combinators.node.AbstractNode;
import com.googlecode.lohr.parser.combinators.node.StringNode;

public final class StringMatcher extends Matcher
{
	private final String _target;

	public StringMatcher(String target)
	{
		_target= target;
	}

	@Override
	public MatchResults match(MatchContext ctx, int start) 
	{
		String input= ctx.getInput();
		int i= 0;
		int l= _target.length();
		int e= input.length();
		while (i < l && (start+i) < e && input.charAt(start + i) == _target.charAt(i))
			i++;
		if (i == l)
			return new MatchResults(Arrays.<AbstractNode>asList(new StringNode(this, start, start + l, _target)));
		return new MatchResults("Expected '"+_target+"'", start + i);
	}
	
	public String getTarget() {
		return _target;
	}
}